# -*- coding:utf-8 -*-
#!/usr/bin/env python

import Tkinter
from Tkinter import *
from functools import partial
from tkMessageBox import showerror,showinfo,showwarning

WARN = "warn"
CRIT = "crit"
REGU = "regu"

#定义指示牌
SIGNS = {
    'do not enter':CRIT,
    'railroad crossing':WARN,
    '55 speed limit':REGU,
    'wrong way':CRIT,
    'mering traffic': WARN,
    'one way':REGU,
}

#按下鼠标后显示的内容
crit_msg = lambda:showerror("Error","Error Button Pressed")
warn_msg = lambda:showwarning("Warn","Warn Button Pressed")
regu_msg = lambda:showinfo("info","Info Button Pressed")

#根窗口
root = Tk()
#设定根窗口的标题
root.title("Road Sign")
#设定根窗口的文本和响应
quit = Button(root,text="quit",command=root.quit,bg='red',fg='white')
quit.pack()

#partial偏函数,提前给函数绑带参数(提前给函数的参数赋上值)
my_button = partial(Button,root)

#生成三个偏函数,每次按下执行匿名函数,command用来做事件的回调(响应按下的操作)
crit_button = partial(my_button,command=crit_msg,bg="white",fg="red")
warn_button = partial(my_button,command=warn_msg,bg="goldenrod1")
regu_button = partial(my_button,command=regu_msg,bg="white")

for each_sign in SIGNS:
    sign_type = SIGNS[each_sign]
    # sign_type.title()
    #分别给这三个偏函数增加文本显示
    #%r 用repr()方法处理对像,regu_button(text='one way'.title()).pack(fill=X,expand=True)
    #%s 用str()方法处理对像, regu_button(text=one way.title()).pack(fill=X,expand=True)
    #用%s 处理的话,text值少了引号,用%r处理,text值有引号
    #%r打印时能够重现它所代表的对象
    #用print cmd可以查看不同
    cmd = '%s_button(text=%s%s).pack(fill=X,expand=True)'% (sign_type.lower(),each_sign,'.upper()' if sign_type == CRIT else '.title()')
    # print cmd
    eval(cmd)

root.mainloop()
